package com.example.fragments;
import com.example.smarthomecontroller.R;

import android.app.Activity;

import android.content.Intent;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.speech.RecognizerIntent;


import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.content.Context;
import com.example.smarthomecontroller.Settings;

public class SpeechFragment extends Fragment {
	
	public static final String ARG_SECTION_NUMBER = "section_number";
	public static final int SPEECH_RECOGNIZED = 1; 
	private WifiManager  wifi;
	Settings settings; 
    Button speechButton;
    TextView chosenText;
    
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		
		View rootView = inflater.inflate(R.layout.speech_fragment, container, false);
        chosenText = (TextView) rootView.findViewById(R.id.chosen_text);        
        chosenText.setText("Starten klappt Text");
        this.speechButton = (Button) rootView.findViewById(R.id.speech_button);
        speechButton.setOnClickListener(speechButtonListener);
 
		return rootView;
	}
	
	
	
	  OnClickListener speechButtonListener = new OnClickListener() {
          public void onClick(final View view){
        	  	Intent intent = new Intent();
      			intent.setAction(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
      			intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
      			Activity act = getActivity();
      					
      		    act.startActivityForResult(intent, SPEECH_RECOGNIZED);    
        	    chosenText.setText(act.getClass().toString());
          }
  };
 
	
	
	 @Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		 Log.d("HIER", "hier");
		super.onActivityResult(requestCode, resultCode, data);
		
		if(requestCode == SpeechFragment.SPEECH_RECOGNIZED)
		{
			chosenText.setText("reuslt erhalten!!!!!");
			
		}
		
	}
	 
	 //int requestCode, int resultCode, Intent data
	public void result(String s) {
		
		
		
		//if(requestCode == SpeechFragment.SPEECH_RECOGNIZED)
		//{
			chosenText.setText("s");
			
		//}
		
	} 





	public void listen(Activity activity)
	{
		Intent intent = new Intent();
		intent.setAction(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
		intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
		activity.startActivityForResult(intent, SPEECH_RECOGNIZED);
		
	}
	
	 
	 


	 private void setWifiManager()
	 {
		 wifi = (WifiManager) settings.context.getSystemService(Context.WIFI_SERVICE);

	 }
	
	
	
	
	
}
